<?php
	//if(!function_exists('mcrypt_module_open'))	require 'xxtea.php';
	include ('xxtea.php');
	function setEncryptCookie($key, $value, $time = 0, $path) {
		setcookie($key, encrypt($value), $time, $path);
	}
	function getEncryptCookie($key) {
		if ( isset($_COOKIE[$key]) ) {
			return decrypt($_COOKIE[$key]);
		} else {
			return null;
		}
	}
	
	function getCookie($key) {
		if ( isset($_COOKIE[$key]) )
			return $_COOKIE[$key];
		else
			return null;
	}
	
	function delCookie($key) {
		setcookie($key, '', time()-300, '/');
	}
	
	function encrypt($plain_text){ 
		if(function_exists('mcrypt_module_open'))
		{
			$td = mcrypt_module_open('blowfish', '', 'cfb', '');
         	$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
          	mcrypt_generic_init($td, SECURE_KEY, $iv);
          	$crypt_text = mcrypt_generic($td, $plain_text);
         	mcrypt_generic_deinit($td);
          	return base64_encode($iv.$crypt_text);
		}else{
			$crypt_text=xxtea_encrypt($plain_text,SECURE_KEY);
			return base64_encode($crypt_text);
		}
	}
	
	function decrypt($crypt_text) {
		$crypt_text = base64_decode($crypt_text);
		if(function_exists('mcrypt_module_open'))
		{
			$td = mcrypt_module_open('blowfish', '', 'cfb', '');
			$ivsize = mcrypt_enc_get_iv_size($td);
			$iv = substr($crypt_text, 0, $ivsize);
			$crypt_text = substr($crypt_text, $ivsize);
			mcrypt_generic_init($td, SECURE_KEY, $iv);
			$plain_text = mdecrypt_generic($td, $crypt_text);
			mcrypt_generic_deinit($td);
		}else{
			$plain_text=xxtea_decrypt($crypt_text,SECURE_KEY);			
		}
		return $plain_text;
	}
	
	if (!function_exists('mb_strlen') ) {
		function mb_strlen($text, $encode) {
			if (strtolower($encode) == 'utf-8') {
				return preg_match_all('%(?:
					[\x09\x0A\x0D\x20-\x7E]     # ASCII
					| [\xC2-\xDF][\x80-\xBF]# non-overlong 2-byte
					|  \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
					| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
					|  \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
					|  \xF0[\x90-\xBF][\x80-\xBF]{2}    # planes 1-3
					| [\xF1-\xF3][\x80-\xBF]{3}   # planes 4-15
					|  \xF4[\x80-\x8F][\x80-\xBF]{2}    # plane 16
				)%xs',$text,$out);
			}else{
				return strlen($text);
			}
		}
	}
?>